package com.lynch.cxsqw;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;

import com.lynch.cxsqw.view.MainActivity;

/**
 * activity基类 
 */
public class BaseActivity extends Activity {

	/**所有的activity*/
	public static List<Activity> activityList = new LinkedList<Activity>();
	
	/**
	 * 初始化的时候添加此activity到activity链表里面
	 */
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		activityList.add(this);
	}
	
	/**
	 * 销毁的时候，销毁掉全部的activity 
	 */
	protected void onDestroy() {
		
		super.onDestroy();
		activityList.remove(this);
	}
	
	/**
	 * 得到屏幕宽度
	 */
	public int getScreenWidth() {

		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		return display.getWidth();
	}
	
	/**
	 * 得到屏幕高度
	 */
	public int getScreenHeight() {

		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		return display.getHeight();
	}
	
	/**
	 * 得到屏幕对象
	 */
	public Display getDisplay() {

		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		return display;
	}
	
	/**
	 * 退出方法 
	 */
	public void exit() {
		
		for(Activity activity : activityList) {
			activity.finish();
		}
		activityList.clear();
	}
	
	/**
	 * 添加菜单
	 */
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);// 指定使用的XML
		return true;
	}

	/**
	 * 处理菜单事件
	 */
	public boolean onOptionsItemSelected(MenuItem item) {

		int item_id = item.getItemId();// 得到当前选中MenuItem的ID
		if(item_id==R.id.menuIndex){
			System.out.println("点击首页");
			forwardToActivity(this,MainActivity.class);
		}else if(item_id==R.id.menuReturn){
			System.out.println("点击返回");
		}else if(item_id==R.id.menuUpdate){
			System.out.println("点击更新");
		}else if(item_id==R.id.menuAbout){
			System.out.println("点击关于");
		}else if(item_id==R.id.menuExit){
			System.out.println("点击退出");
			exit();
		}
		
		return true;
	}
	
	/**
	 * 返回按钮
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

			dialog(this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 弹出对话框 
	 */
	protected void dialog(Context context) {
		
		AlertDialog.Builder builder = new Builder(context);
		builder.setTitle("提示");
		builder.setMessage("确认退出吗？");
		builder.setPositiveButton("确定", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				exit();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	/**
	 * 跳转activity 
	 */
	public void forwardToActivity(Context context, Class nextActivity){
		
		Intent intent = new Intent(context,nextActivity);
		startActivity(intent);
	}
}
